package jejunu.androboys.QuickCall;

import jejunu.androboys.QuickCall.R;
import jejunu.androboys.QuickCall.Component.FileController;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MotionModeActivity extends Activity 
{	
	double x1=0;
	double y1=0;
	double x2=0;
	double y2=0;
	double x3=0;
	double y3=0;
	
	private String[][] mSave_data;
	
	LinearLayout motion_layout;
	Button bt_center;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.motion_mode);
	    
	    
	}
	
	@Override
	protected void onResume() 
	{
		FileController fc = new FileController(this);
	    mSave_data=fc.loadListFile();
	    
	    motion_layout=(LinearLayout) findViewById(R.id.MotionMode_lout);
	    motion_layout.setBackgroundResource(R.drawable.back1);
	    
	    bt_center=(Button) findViewById(R.id.MotionMode_bt_center);
	    bt_center.setVisibility(View.INVISIBLE);
	    
		super.onResume();
	}
	
    public double getGradient(double x1, double y1, double x2, double y2)
    {
    	double x_quantity=x1-x2;
    	double y_quantity=y1-y2;
    	
//    	int gradient=y_quantity/x_quantity;
    	
    	double fx=0-1;
    	double fy=0-2;
    	
    	double gradient=Math.atan2(y_quantity, x_quantity)-Math.atan2(fx, fy);
    	
    	return gradient;
    }
    
    public double[] getBaseArray()
    {
    	double[] BaseArray=new double[8];
    	
    	BaseArray[0]=getGradient(0, 0, 2, 1);//시작
        BaseArray[1]=getGradient(0, 0, 1, 2);//끝 1번 시작
        BaseArray[2]=getGradient(0, 0, -1, 2);//끝 2번 시작
        BaseArray[3]=getGradient(0, 0, -2, 1);//끝 3번 시작
        BaseArray[4]=getGradient(0, 0, -2, -1);//끝 4번 시작
        BaseArray[5]=getGradient(0, 0, -1, -2);//끝 5번 시작
        BaseArray[6]=getGradient(0, 0, 1, -2);//끝 6번 시작
        BaseArray[7]=getGradient(0, 0, 2, -1);//끝 7번
        //나머지는 8번
        
//        Log.v("", "1사분면 22도 : "+BaseArray[0]);
//        Log.v("", "1사분면 67도 : "+BaseArray[1]);
//        Log.v("", "2사분면 112 : "+BaseArray[2]);
//        Log.v("", "2사분면 157 : "+BaseArray[3]);
//        Log.v("", "3사분면 112 : "+BaseArray[4]);
//        Log.v("", "3사분면 157 : "+BaseArray[5]);
//        Log.v("", "4사분면 112 : "+BaseArray[6]);
//        Log.v("", "4사분면 157 : "+BaseArray[7]);
        
        return BaseArray;
    }
    
    private int findRange(double a)
    {
    	int num;
    	
    	double[] array = getBaseArray();
		
		if(a > array[0] && a < array[1])
		{
			num = 0;
		}
		else if(a > array[1] && a < array[2] ){
			num = 1;
		}
		else if(a > array[2] && a < array[3]){
			num = 2;
		}
		else if(a > array[3] && a < array[4]){
			num = 3;
		}
		else if(a > array[4] && a < array[5]){
			num = 4;
		}
		else if(a > array[5] && a < array[6]){
			num = 5;
		}
		else if(a > array[6] && a < array[7]){
			num = 6;
		}
		else {   	
			num = 7;
		}
		
		return num;
    }
	
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		
		
		if(event.getAction()==MotionEvent.ACTION_MOVE)
		{
			int[] img=new int[]{R.drawable.one, R.drawable.two, R.drawable.three,
					R.drawable.four, R.drawable.five, R.drawable.six,
					R.drawable.seven, R.drawable.eight};
			
			x3=event.getX();
    		y3=event.getY();
    		double a=getGradient(x1, y1, x3, y3);
    		
    		Log.v("", "무브 이벤트 : "+a);
    		
    		int num=findRange(a);
    		bt_center.setVisibility(View.VISIBLE);
    		bt_center.setBackgroundResource(img[num]);
//    		
//    		motion_layout.setBackgroundResource(img[num]);
		}
		else	if(event.getAction()==MotionEvent.ACTION_DOWN)
    	{
    		x1=event.getX();
    		y1=event.getY();
    		
    		Log.v("", "x : "+x1+" y : "+y1);
    	}
    	else if(event.getAction()==MotionEvent.ACTION_UP)
    	{
    		x2=event.getX();
    		y2=event.getY();
    		
    		double a=getGradient(x1, y1, x2, y2);
    		
        	Log.v("", "터치 이벤트 : "+a);
        	
        	Uri number;
    		Intent intent;
    		
    		int num=findRange(a);
    		
    		number = Uri.parse("tel:" + mSave_data[num][1]);
    		
    		intent = new Intent(Intent.ACTION_DIAL, number);
    		startActivity(intent);
    	}
    	
    	return true;
	}
}
